﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using PlugNT.Safe;
using PlugNT.Database;
using PlugNT.Database.Common.Simple;
using PlugNT.Custom;
using PlugNT.Cms.Model;

namespace PlugNT.Cms.DAL
{
    public class Advertise
    {
        private static string currTableName = WebConfig.TablePrefix + "advertise";



        public AdvertiseInfo GetAdvertiseInfo(DataRow dr)
        {
            AdvertiseInfo model = new AdvertiseInfo();
            model.id = (int)dr["id"];
            model.name = (string)dr["name"];
            model.power = (int)dr["power"];
            model.statbrowse = (int)dr["statbrowse"];
            model.statclick = (int)dr["statclick"];
            model.overduetime = (DateTime)dr["overduetime"];
            model.examine = (int)dr["examine"];
            model.upspreadadd = (string)dr["upspreadadd"];
            model.sizebreadth = (string)dr["sizebreadth"];
            model.hight = (string)dr["hight"];
            model.linkaddress = (string)dr["linkaddress"];
            model.hint = (string)dr["hint"];
            model.backgortarget = (int)dr["backgortarget"];
            model.advertisecont = (string)dr["advertisecont"];
            model.browsecount = (int)dr["browsecount"];
            model.clickcount = (int)dr["clickcount"];
             model.adtype = (int)dr["adtype"];
             model.Colname = (dr["Colname"] != DBNull.Value) ? (string)dr["ColName"] : null;
            model.Colno = (dr["Colno"] != DBNull.Value) ? (string)dr["ColNo"] : null;
            return model;
        }


        public AdvertiseInfo GetAdvertiseInfo(int id, string currPostfix)
        {
            AdvertiseInfo model = null;

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 * from " + currTableName + currPostfix);
            strSql.Append(" where id=" + id.ToString());
            DataTable dt = DbHelper.TabQuery(strSql.ToString());
            if (dt.Rows.Count > 0)
            {
                model = GetAdvertiseInfo(dt.Rows[0]);
            }
            return model;
        }










        public bool Add(AdvertiseInfo model, string currPostfix)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into " + currTableName + currPostfix + "(");
            strSql.Append("[name], [power], [statbrowse], [statclick], [overduetime], [examine], [upspreadadd], [sizebreadth], [hight], [linkaddress], [hint], [backgortarget], [advertisecont], [browsecount], [clickcount], [adtype],[ColName],[ColNo] ");
            strSql.Append(")");
            strSql.Append(" values (");

            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.name) + "',");
            strSql.Append("'" + model.power.ToString() + "',");
            strSql.Append("'" + model.statbrowse.ToString() + "',");
            strSql.Append("'" + model.statclick.ToString() + "',");
            strSql.Append("getdate(),");
            strSql.Append("'" + model.examine.ToString() + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.upspreadadd) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.sizebreadth) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.hight) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.linkaddress) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.hint) + "',");
            strSql.Append("'" + model.backgortarget.ToString() + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.advertisecont) + "',");
            strSql.Append("'" + model.browsecount.ToString() + "',");
            strSql.Append("'" + model.clickcount.ToString() + "',");
            strSql.Append("'" + model.adtype.ToString() + "',");
            strSql.Append("'" + model.Colname.ToString() + "',");
            strSql.Append("'" + model.Colno.ToString() + "'");

            strSql.Append(")");
            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }


        public bool Update(AdvertiseInfo model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName);
            strSql.Append(" set [name]='" + StringHelper.SqlFilter(model.name) + "',");
            strSql.Append("[power]='" + model.power + "',");
            strSql.Append("[statbrowse]='" + model.statbrowse + "',");
            strSql.Append("[statclick]='" + model.statclick + "',");
            strSql.Append(" [overduetime]='" + model.overduetime + "',");
            strSql.Append("[examine]='" + model.examine + "',");
            strSql.Append("[upspreadadd]='" + StringHelper.SqlFilter(model.upspreadadd) + "',");
            strSql.Append("[sizebreadth]='" + StringHelper.SqlFilter(model.sizebreadth) + "',");
            strSql.Append("[hight]='" + StringHelper.SqlFilter(model.hight) + "',");
            strSql.Append("[linkaddress]='" + StringHelper.SqlFilter(model.linkaddress) + "',");
            strSql.Append("[hint]='" + StringHelper.SqlFilter(model.hint) + "',");
            strSql.Append("[backgortarget]='" + model.backgortarget + "',");
            strSql.Append("[advertisecont]='" + model.advertisecont + "',");
            strSql.Append("[browsecount]='" + model.browsecount + "',");
            strSql.Append("[clickcount]='" + model.clickcount + "',");
            strSql.Append("[adtype] ='" + model.adtype + "', ");
            strSql.Append("[ColName] ='" + model.Colname + "', ");
            strSql.Append("[ColNo] ='" + model.Colno + "' ");
            strSql.Append(" where id=" + model.id.ToString());
            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }


        public bool UpdateClickCount(AdvertiseInfo model, string currPostfix)
        {

            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName + currPostfix + " set ");
            strSql.Append("clickcount=" + model.clickcount.ToString());
            strSql.Append(" where id=" + model.id.ToString());

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }


        public bool AddClickCount(int id, string currPostfix)
        {

            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName + currPostfix + " set ");
            strSql.Append("clickcount=clickcount+1");
            strSql.Append(" where id=" + id.ToString());

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }








        public bool Delete(int id, string currPostfix)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from " + currTableName + currPostfix);
            strSql.Append(" where id=" + id.ToString());
            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }

        public DataTable GetList(int topCount, string andWhere, string currPostfix)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top " + topCount.ToString() + " * from  [" + currTableName + currPostfix + "] where 1=1 ");
            strSql.Append(andWhere);
            return DbHelper.TabQuery(strSql.ToString());
        }

        public DataTable GetList(int pageSize, int currPage, string andWhere, bool showSql, string currPostfix)
        {

            string sql = Sql.GetRePagingString(pageSize, currPage, "[" + currTableName + currPostfix + "]", "id", andWhere);
            if (showSql) //调试
            {
                System.Web.HttpContext.Current.Response.Write(sql);
            }
            return DbHelper.TabQuery(sql);
        }

        public string GetObjectString(string colname, string andWhere, string currPostfix)
        {

            Object obj = DbHelper.GetSingle(string.Format("select top 1 [{0}] from [" + currTableName + currPostfix + "] where 1=1 {1}", colname, andWhere));
            return obj == null ? "" : obj.ToString();
        }

        public DataTable GetIdList(string andWhere, string currPostfix)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select [id] from [" + currTableName + currPostfix + "] where 1=1 ");
            strSql.Append(andWhere);
            return DbHelper.TabQuery(strSql.ToString());
        }

        public DataTable GetList(int pageSize, int currPage, string columns, string currPostfix, Object joininfo)
        {

            string sql = Sql.GetRePagingString(pageSize, currPage, currTableName + currPostfix, "id", columns, (JoinInfo)joininfo);
            return DbHelper.TabQuery(sql);
        }




        public int GetCount(string andWhere, string currPostfix)
        {
            string sql = "select count(1) from [" + currTableName + currPostfix + "] where 1=1 " + andWhere;
            return (int)DbHelper.GetSingle(sql);
        }

        public int GetClickCount(string andWhere, string currPostfix)
        {

            string sql = "select clickcount from [" + currTableName + currPostfix + "] where 1=1 " + andWhere;
            return (int)DbHelper.GetSingle(sql);
        }




        public string Help()
        {
            return @"mailto:354990393@qq.com";
        }


    }
}
